#ifndef _DSWCONSOLE_BASE_H
#define _DSWCONSOLE_BASE_H

#include <libbase/dswbase.h>
#include <fcntl.h>
#include <termios.h>

typedef enum{
	CON_SESSION_PTZ = 0,
	CON_SESSION_GRPS = 1,
} EN_CON_SESSION_TYPE;

#define MAX_CONSOLE_BUFFER_SIZE (5 * 1024)

typedef struct{
	char nData[MAX_CONSOLE_BUFFER_SIZE];
	char* pPos;
} ST_COMM_RECV_T, *LPST_COMM_RECV_T;

class CDswconsole_base : public CDswbase
{
public:
	CDswconsole_base(LPST_ENV_T pEnv, EN_CON_SESSION_TYPE nType, LPST_CONSOLE_INFO_T pInfo);
	virtual ~CDswconsole_base();
	bool IsValid(){return (m_nConsole != -1);};
private:
	virtual bool SeekToStartComm(LPST_COMM_RECV_T pCommRecv, int &nPayloadSize);
	virtual void ProcessData(LPST_COMM_RECV_T pCommRecv){};
	virtual bool CheckValidData(LPST_COMM_RECV_T pCommRecv){return false;};
protected:
	EN_CON_SESSION_TYPE GetSessionType(){return m_nType;};
	void ReOpen();
	virtual void ResetBuffer();
private:
	int OpenConsole(EN_CON_SESSION_TYPE nType);
	bool SetConsoleOpt(int nfd, int nspeed, int nbits, char nevent, int nstop);

	static void cb_OnRead(struct bufferevent *bev, void *ctx){((CDswconsole_base*)ctx)->OnRead(bev);};
	virtual void OnRead(struct bufferevent *bev);
	static void cb_OnEvent(struct bufferevent *bev, short events, void *ctx){((CDswconsole_base*)ctx)->OnEvent(bev, events);};
	virtual void OnEvent(struct bufferevent *bev, short events){};

protected:
	EN_CON_SESSION_TYPE m_nType;
	int m_nConsole;
	struct bufferevent *m_pBev;
	int m_nWantSize;
	bool m_bStartComm;
	ST_COMM_RECV_T m_nCommRecv;
	ST_CONSOLE_INFO_T m_nConsoleInfo;
};

#endif

